<?php

/*

Process Mail (mail_send.pro.php)

*/
ob_start();
$interactive = 1;
$rank_check = 3;
include "global.inc.php";

if ($getInfo[premium] == "1")
{
	$colors = "$text_color, $bg_color, $bg_image.gif";

	$remember = fetch("SELECT background,text FROM remember_colors WHERE userid = '$userid'");
	if (($remember[background] != "$bg_color") OR ($remember[text] != "$text_color"))
	{
		$bg_color2 = ereg_replace("#", "", $bg_color);
		$text_color2 = ereg_replace("#", "", $text_color);

		mysql_query("DELETE FROM remember_colors WHERE userid = '$userid'");
		mysql_query("INSERT INTO remember_colors VALUES ('', '$userid', '$bg_color2', '$text_color2')");
	}
}

$send_to = strtolower(ereg_replace(" ","",$send_to));
$findTo = fetch("SELECT id,display_name,premium FROM members2 WHERE username = '$send_to' AND game = '$game'");
if (!$findTo[id]) { die(header(error("mail_send.php?game=$game&type=$type&id=$id", "The user you are trying to send this to does not exsist."))); }
$findTo2 = fetch("SELECT mail_settings FROM members_profiles2 WHERE username = '$send_to' AND game = '$game'");

if ($findTo[premium] == 0) { $max = 50; }
if ($findTo[premium] == 1) { $max = 250; }

if ($findTo2[mail_settings] == 2)
{
	die(header(error("mail_send.php?game=$game&type=$type&id=$id", "Sorry, this user does not except mails right now.")));
}
if ($findTo2[mail_settings] == 1)
{
	$findBud = fetch("SELECT * FROM buds2 WHERE you = '$userid' AND me '$findTo[id]' AND game = '$game'");
	if (!$findBud[id])
	{
		die(header(error("mail_send.php?game=$game&type=$type&id=$id", "Sorry, that user only except messages from their buddies.")));
	}
}

$findBlocked = fetch("SELECT id FROM blocked_users2 WHERE you = '$userid' AND me = '$findTo[id]' AND game = '$game'");

if ($findBlocked[id])
{
	die(header(error("mail_send.php?game=$game&type=$type&id=$id", "Sorry, that user only except messages from their buddies.")));
}
$numMessages = mysql_num_rows(mysql_query("SELECT id FROM mail_topics2 WHERE to_user = '$findTo[id]' AND game = '$game'"));
if ($numMessages >= $max)
{
	die(header(error("mail_send.php?game=$game&type=$type&id=$id", "Sorry, this user has filled it's $max mail limit!")));
}

if ((!$subject) OR (!$message))
{
	die(header(error("mail_send.php?game=$game&type=$type&id=$id", "Please do not leave the subject or message blank.")));
}

mysql_query("INSERT INTO mail_topics2 (to_user,from_user,subject,timestamp,game) VALUES ('$findTo[id]','$userid','$subject','$timestamp','$game')");

$insert_id = mysql_insert_id();

mysql_query("INSERT INTO mail_messages2 (mail_id,to_user,message,game,colors) VALUES ('$insert_id','$findTo[id]','$message','$game','$colors')");

systemmessage("$game", "mail_view.php?game=$game&id=$insert_id", "$findTo[id]", "You got a new mail \"$subject\" from $display_name!");

header(error("mail.php?game=$game","Your message has been sent to $findTo[display_name]."));

?>